﻿using BLL;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace ShoppingWebSite
{
    public partial class myShopping : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if(Session["UserId"] == null)
            {
                Response.Write("<script>alert('未登录不可查看')</script>");
                Response.Redirect("homeWeb.aspx");
            }
            else
            {
                if (!IsPostBack)
                {
                    string sql = string.Format("select OId,URealName,PSName,OTime,PPrice,OTypeName from [dbo].[Order] o" +
                    ",UserName u,Product p,OrderType t where o.OState=t.OTypeId and o.OUserId=u.UId and o." +
                    "OProductId=p.PId and OState={0} and UId={1}", 1, Session["UserId"].ToString());
                    gvOrder.DataSource = BLLAllOperation.BLL_GetDataTable(sql);
                    gvOrder.DataBind();
                }
            }
            
        }

        protected void gvOrder_RowEditing(object sender, GridViewEditEventArgs e)
        {
            gvOrder.EditIndex = e.NewEditIndex;
            string sql = string.Format("select OId,URealName,PSName,OTime,PPrice,OTypeName from [dbo].[Order] o" +
                ",UserName u,Product p,OrderType t where o.OState=t.OTypeId and o.OUserId=u.UId and o." +
                "OProductId=p.PId and OState={0} and UId={1}", 1, Session["UserId"].ToString());
            gvOrder.DataSource = BLLAllOperation.BLL_GetDataTable(sql);
            gvOrder.DataBind();
        }

        protected void gvOrder_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            string index = gvOrder.Rows[e.RowIndex].Cells[0].Text;
            string sqlupdate = string.Format("Update [dbo].[Order] set OState=2 where OId={0}", index);
            if (BLLAllOperation.BLL_GetExecuteNonQuery(sqlupdate))
            {
                Response.Write("<script>alert('付款成功')</script>");
            }
            else
            {
                Response.Write("<script>alert('付款失败')</script>");
            }
            gvOrder.EditIndex = -1;
            string sql = string.Format("select OId,URealName,PSName,OTime,PPrice,OTypeName from [dbo].[Order] o" +
                ",UserName u,Product p,OrderType t where o.OState=t.OTypeId and o.OUserId=u.UId and o." +
                "OProductId=p.PId and OState={0} and UId={1}", 1, Session["UserId"].ToString());
            gvOrder.DataSource = BLLAllOperation.BLL_GetDataTable(sql);
            gvOrder.DataBind();
        }

        protected void gvOrder_RowUpdated(object sender, GridViewUpdatedEventArgs e)
        {
            gvOrder.EditIndex = -1;
            string sql = string.Format("select OId,URealName,PSName,OTime,PPrice,OTypeName from [dbo].[Order] o" +
                ",UserName u,Product p,OrderType t where o.OState=t.OTypeId and o.OUserId=u.UId and o." +
                "OProductId=p.PId and OState={0} and UId={1}", 1, Session["UserId"].ToString());
            gvOrder.DataSource = BLLAllOperation.BLL_GetDataTable(sql);
            gvOrder.DataBind();
        }

        protected void gvOrder_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            string sqldelete = string.Format("delete from [dbo].[Order] where OId={0}", gvOrder.Rows[e.RowIndex].Cells[0].Text);
            if (BLLAllOperation.BLL_GetExecuteNonQuery(sqldelete))
            {
                Response.Write("<script>alert('删除成功')</script>");
            }
            else
            {
                Response.Write("<script>alert('删除失败')</script>");
            }
            string sql = string.Format("select OId,URealName,PSName,OTime,PPrice,OTypeName from [dbo].[Order] o" +
                ",UserName u,Product p,OrderType t where o.OState=t.OTypeId and o.OUserId=u.UId and o." +
                "OProductId=p.PId and OState={0} and UId={1}", 1, Session["UserId"].ToString());
            gvOrder.DataSource = BLLAllOperation.BLL_GetDataTable(sql);
            gvOrder.DataBind();
        }

        protected void gvOrder_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            gvOrder.EditIndex = -1;
            string sql = string.Format("select OId,URealName,PSName,OTime,PPrice,OTypeName from [dbo].[Order] o" +
                ",UserName u,Product p,OrderType t where o.OState=t.OTypeId and o.OUserId=u.UId and o." +
                "OProductId=p.PId and OState={0} and UId={1}", 1, Session["UserId"].ToString());
            gvOrder.DataSource = BLLAllOperation.BLL_GetDataTable(sql);
            gvOrder.DataBind();
        }

        protected void gvOrder_SelectedIndexChanged(object sender, EventArgs e)
        {

        }
    }
}